package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPayPassword;
import com.digitalchina.lxnsy.api.BusinessWallet;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MyWalletVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.ExpertInfoXML;
import com.digitalchina.lxnsy.xml.PersonalInfoXML;
import com.digitalchina.lxnsy.xml.UserXML;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/10/8 14:42
 * 修改备注：
 * ================================================
 */
public class MyWalletActivity extends BaseActivity {
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_balance)
    TextView tvBalance;
    @Bind(R.id.tv_freeze)
    TextView tvFreeze;
    @Bind(R.id.layout_freeze)
    LinearLayout layoutFreeze;
    @Bind(R.id.dv_header)
    SimpleDraweeView dvHeader;
    @Bind(R.id.bt_pay)
    Button btPay;
    @Bind(R.id.bt_withdraw)
    Button btWithdraw;

    private MyWalletVO myWalletVO;

    private boolean mHavePayPassword = false;


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_my_wallet);
        setTitle("我的钱包");
        setRightText("交易记录");
    }

    @Override
    protected void onResume() {
        super.onResume();
        BusinessWallet.getBalance(this, mHandler);
    }

    @Override
    public void initMember() {


        String name = "";
        String img = "";
        if (UserXML.getUserType(this).equals(Constant.USER_TYPE_EXPERT)) {
            //专家用户
            name = ExpertInfoXML.getExpertName(this);
            img = ExpertInfoXML.getPhoto(this);
        } else {
            //个人用户
            if (!StringUtil.isStrEmpty(UserXML.getUserTrueName(this)))
                name = UserXML.getUserTrueName(this);
            else
                name = UserXML.getLoginName(this);

            img = PersonalInfoXML.getHeadportrait(this);

        }

        FrescoUtil.showImageMid(img, dvHeader);

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_GET_BALANCE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_BALANCE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_HAVE_PAY_PASSWORD_SUCCESS: // 查询用户是否设置支付密码
                        String havePayPassword = FastJsonUtil.getString(msg.obj.toString(), "havePayPassword");

                        if (havePayPassword.equals("0")) {
                            mHavePayPassword = false;
                            new MaterialDialog.Builder(MyWalletActivity.this)
                                    .title("提示")
                                    .content("您还没有设置过提现密码，是否现在设置？")
                                    .positiveText("去设置")
                                    .negativeText("取消")
                                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            GotoUtil.gotoActivity(MyWalletActivity.this, SetPayPasswordOneActivity.class, "type", 0);
                                        }
                                    })
                                    .show();
                        } else {
                            mHavePayPassword = true;
                            GotoUtil.gotoActivity(MyWalletActivity.this, WithdrawMoneyActivity.class, "myWalletVO", myWalletVO);
                        }

                        break;
                    case MSG.MSG_HAVE_PAY_PASSWORD_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        LogUtils.e(object);

        myWalletVO = FastJsonUtil.getBean(object.toString(), "", MyWalletVO.class);

        /**
         * actualBal    余额
         * freezeMoney 总共冻结保障金(忽略）
         * canUseBal  可用余额
         */

        if (myWalletVO != null) {
            tvPrice.setText("￥" + myWalletVO.getActualBal());
            tvBalance.setText(myWalletVO.getCanUseBal());
            tvFreeze.setText(myWalletVO.getFreezeMoney());
        }

    }

    @Override
    public void rightOnClick() {
        go(TradeRecordListActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_freeze, R.id.bt_pay, R.id.bt_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_freeze:
                break;
            case R.id.bt_pay:
                break;
            case R.id.bt_withdraw:

                if (myWalletVO != null) {
                    if (mHavePayPassword)
                        GotoUtil.gotoActivity(MyWalletActivity.this, WithdrawMoneyActivity.class, "myWalletVO", myWalletVO);
                    else
                        BusinessPayPassword.havePayPassword(this, mHandler);
                } else {
                    showToast("获取余额失败，请重试！");
                }


                break;
        }
    }
}
